#include "bamboossh.h"

#include <iostream>
#include <stdio.h>
#include <stdlib.h>

#include <winsock2.h>
#include <ws2tcpip.h>
int socket_connect(const char *hostname, int port) {
    //初始化WSA
    WORD sockVersion=MAKEWORD(2,2);
    WSADATA wsaData;//WSADATA结构体变量的地址值

    //int WSAStartup(WORD wVersionRequested, LPWSADATA lpWSAData);
    //成功时会返回0，失败时返回非零的错误代码值
    if(WSAStartup(sockVersion,&wsaData)!=0)
    {
        std::cout << "WSAStartup() error!" << std::endl;
        return 0;
    }
    //创建套接字
    SOCKET slisten=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    if(slisten==INVALID_SOCKET)
    {
        std::cout << "socket error!" << std::endl;
        return 0;
    }
    //绑定IP和端口
    sockaddr_in sin;//ipv4的指定方法是使用struct sockaddr_in类型的变量
    sin.sin_family = AF_INET;
    sin.sin_port = htons(port);//设置端口。htons将主机的unsigned short int转换为网络字节顺序
    //sin.sin_addr.S_un.S_addr = inet_addr(hostname);//IP地址设置成INADDR_ANY，让系统自动获取本机的IP地址
    //bind函数把一个地址族中的特定地址赋给scket。
    if (inet_pton(AF_INET, hostname, &sin.sin_addr) <= 0) {
        std::cout << "bind error!" << std::endl;
        closesocket(slisten);
        WSACleanup();
        return -1;
    }
    if (connect(slisten, (sockaddr*)&sin, sizeof(sin)) == SOCKET_ERROR) {
        std::cerr << "Connection failed: " << WSAGetLastError() << std::endl;
        closesocket(slisten);
        WSACleanup();
        return 1;
    }
    //std::cout << "Connected to server!" << std::endl;
    return slisten;
}
BambooSsh::BambooSsh() {

}
int BambooSsh::connect(const char *host,const int port) {

    sshInit = libssh2_init(0);
    if(sshInit != 0){
        fprintf(stderr, "Failed to initialize libssh2. Error code %d", sshInit);
    }else{
        session = libssh2_session_init();
        if(session == NULL){
            fprintf(stderr,"Failed to create session");
            return -1;
        }
        //设置会话阻塞模式
        //1阻塞，0非阻塞
        int allowBlocked = 1;
        libssh2_session_set_blocking(session,allowBlocked);
    }
    int socket = socket_connect(host,port);
    if (socket < 0) {
        fprintf(stderr, "Failed to connect to the remote host");
        return -2;
    }else{
        int rc = libssh2_session_handshake(session, socket);
        if (rc != 0) {
            fprintf(stderr, "Session handshake failed");
            return -3;
        }

    }
    return 1;
}
int BambooSsh::execRemoteCommand(const char *commandName) {

    int rc = libssh2_channel_exec(channel, commandName);
    if (rc != 0) {
        fprintf(stderr, "Failed to execute remote command");
        return -1;
    }else{

        // 读取并打印远程命令输出
        char buffer[1024];
        int nbytes;
        while ((nbytes = libssh2_channel_read(channel, buffer, sizeof(buffer) - 1)) > 0) {
            buffer[nbytes] = '\0';
            printf("%s", buffer);
        }

    }
    return 1;
}
int BambooSsh::auth(const char *userName,const char *password) {

    int rc = libssh2_userauth_password(session, userName, password);
    if (rc != 0) {
        fprintf(stderr, "Authentication failed");
    }else{
        channel = libssh2_channel_open_session(session);
        if (channel == NULL) {
            fprintf(stderr, "Failed to open a channel");
        }
    }
    return rc;
}
BambooSsh::~BambooSsh() {

    // 断开连接和清理
    if(channel != NULL){
        libssh2_channel_close(channel);
        libssh2_channel_free(channel);
    }
    if(session != NULL){
        libssh2_session_disconnect(session, "Goodbye");
        libssh2_session_free(session);
    }
    libssh2_exit();

}

int testMain(){

    const char *hostName = "175.178.113.156";
    const int hostPort = 22;
    const char *userName = "root";
    const char *password = "";

    BambooSsh *ssh = new BambooSsh();
    return 0;
}
